package com.initial.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.initial.entity.TbCrmEmployee;
import com.initial.entity.TbCrmTask;
import com.initial.entity.TbSystemUser;
import com.initial.service.TbCrmTaskService;
import com.initial.service.TbSystemUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TaskController {
    @Resource
    private TbCrmTaskService tbCrmTaskService;
    @Resource
    private TbSystemUserService tbSystemUserService;
    /**
     * 去查看   分页查询   任务表
     * */
    @RequestMapping("manage/task/ToFindByPage")
    public String tolist_task(){
        return "views/manage/task/list_task";
    }
    /**
     * 任务表  分页查询
     * */
    @RequestMapping("manage/task/TaskAction_findByPage.action")
    @ResponseBody
    public Map TaskAction_findByPage(Integer page,Integer rows,String asd,String subject){
        Map map=new HashMap();
        if (asd!=null && asd.equals("1")){
            asd="subject";
        }
        List<TbCrmTask> tbCrmTasks = tbCrmTaskService.selectAllPage(page,rows,asd,subject);
        int size = tbCrmTasks.size();
        map.put("totalRows",size);
        map.put("result",tbCrmTasks);
        return map;
    }
    /**
     * 去  添加新任务页面
     * */
    @RequestMapping("manage/task/TaskAction_add.action")
    public String TaskAction_edit(Model model, HttpSession session){
        List<TbSystemUser> tbSystemUsers = tbSystemUserService.getBaseMapper().selectList(null);
        model.addAttribute("tbSystemUsers",tbSystemUsers);
        return "views/manage/task/edit_task";
    }
    /**
     *添加任务
     * */
    @RequestMapping("/manage/task/TaskAction_save.action")
    @ResponseBody
    public Map TaskAction_saveOrUpdat(TbCrmTask tbCrmTask){
        Map map=new HashMap();
        if (tbCrmTask.getCreatorUserId()==-1 && tbCrmTask.getOwnerUserId()==-1 && tbCrmTask.getPriority()==-1){
            map.put("success",false);
            map.put("msg","请选择创建、负责人和优先级");
            return map;
        }
        boolean save = tbCrmTaskService.save(tbCrmTask);
        if (save){
            map.put("success",save);
            map.put("msg","添加成功");
        }else {
            map.put("success",save);
            map.put("msg","添加失败");
        }
        return map;
    }
    /**
     * 删除任务
     * */
    @RequestMapping("manage/task/TaskAction_del.action")
    @ResponseBody
    public Map TaskAction_del(String[] ids,HttpSession session){
        Map map=new HashMap();
        TbSystemUser user = (TbSystemUser) session.getAttribute("user");
        Integer userId = user.getId();
        Date date = new Date();
        Integer integer=null;
        for (String id : ids) {
            Integer i = Integer.parseInt(id);
            integer = tbCrmTaskService.deleteByIdInteger(i);
            UpdateWrapper<TbCrmTask> updateWrapper=new UpdateWrapper<>();
            updateWrapper.set("delete_user_id",userId);
            updateWrapper.set("delete_status",1);
            updateWrapper.set("delete_time",date);
            updateWrapper.eq("id",i);
            tbCrmTaskService.update(updateWrapper);
        }
        if (integer==1){
            map.put("success",true);
            map.put("msg","删除成功");
        }else {
            map.put("success",false);
            map.put("msg","删除失败");
        }
        return map;
    }
    /**
     * 去修改任务页面
     * */
    @RequestMapping("manage/task/TaskAction_edit.action")
    public String TaskAction_edit(Integer id,Model model){
        TbCrmTask tbCrmTask = tbCrmTaskService.selectByIdTbCrmTask(id);
        List<TbSystemUser> tbSystemUsers = tbSystemUserService.getBaseMapper().selectList(null);
        model.addAttribute("tbSystemUsers",tbSystemUsers);
        model.addAttribute("tbCrmTask",tbCrmTask);
        return "views/manage/task/modify_task";
    }
    /**
     * 修改任务信息
     * */
    @RequestMapping("manage/task/TaskAction_Update.action")
    @ResponseBody
    public Map TaskAction_Update(TbCrmTask tbCrmTask){
        Map map=new HashMap();
        if (tbCrmTask.getCreatorUserId()==-1 && tbCrmTask.getOwnerUserId()==-1 && tbCrmTask.getPriority()==-1){
            map.put("success",false);
            map.put("msg","请选择创建、负责人和优先级");
            return map;
        }
        boolean b = tbCrmTaskService.updateById(tbCrmTask);
        if (b){
            map.put("success",b);
            map.put("msg","修改成功");
        }else {
            map.put("success",b);
            map.put("msg","修改失败");
        }
        return map;
    }

    /**
     * 去查看任务详细信息页面
     * */
    @RequestMapping("manage/task/TaskAction_see.action")
    public String TaskAction_see(Integer id, Model model){
        TbCrmTask tbCrmTask = tbCrmTaskService.selectByIdTbCrmTask(id);
        List<TbSystemUser> tbSystemUsers = tbSystemUserService.getBaseMapper().selectList(null);
        model.addAttribute("tbSystemUsers",tbSystemUsers);
        model.addAttribute("tbCrmTask",tbCrmTask);
        return "views/manage/task/see_task";
    }
}
